<%@ page contentType="text/html; charset=utf-8" language="java"
	import="java.sql.*" errorPage=""%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Island Auctions - User Profile</title>
</head>

<body>
<table width="100%" height="100%" border="2">
  <tr>
    <td width="22%" height="95"><img src="logo.png" width="163" height="154" alt="Logo" /></td>
    <td width="78%"><h1>Island Auctions</h1>
    <p> Turning Junk Into Money</p></td>
  </tr>
  <tr>
    <td valign="top"><p><a href="index.jsp">Home</a></p>
    <p><a href="Browse.jsp">Browse</a></p>
    <p><a href="index.jsp">Search</a></p>
    <p><a href="profile.jsp">Profile</a></p>
    <p><a href="Users.jsp">Users</a></p>
    <%
    try{
	    if(session.getValue("login") != null)
	    {%>
	    	<p><a href="Logout.jsp">Logout</a></p>
	    <%}
	    else
	    {%>
	    	<p><a href="login.jsp">Login</a></p>
	    <%}
    }catch(java.lang.IllegalStateException e)
    {%>
		<p><a href="login.jsp">Login</a></p><%
}%>
    
			<td valign="top">
				<%
String mysJDBCDriver = "com.mysql.jdbc.Driver"; 
String mysURL = "jdbc:mysql://mysql2.cs.stonybrook.edu:3306/jedale"; 
String mysUserID = "jedale"; 
String mysPassword = "107200855";
    
String Id = ""+session.getValue("login");
String UserName = request.getParameter("UserName");
if(UserName != null)
	Id = UserName;

if(Id == null || Id == "" || Id == "null")
	response.sendRedirect("index.jsp");
else
{
  			java.sql.Connection conn=null;
  			
  			
			try 
			{
            	Class.forName(mysJDBCDriver).newInstance();
    			java.util.Properties sysprops=System.getProperties();
    			sysprops.put("user",mysUserID);
    			sysprops.put("password",mysPassword);
        
				//connect to the database
            			conn=java.sql.DriverManager.getConnection(mysURL,sysprops);
            			System.out.println("Connected successfully to database using JConnect");
            
            			java.sql.Statement stmt1=conn.createStatement();
        
					java.sql.ResultSet rs = stmt1.executeQuery("SELECT Customer.Rating	FROM Person, Customer WHERE  Customer.CustomerID = Person.SSN AND Person.Username = '" +Id+"'");
    			
					 %>
				<h1>User Profile</h1>
				<br> <%=Id%><br> <%
				if(rs.next())
				{
					%>
						Rating: <%=rs.getString(1)%><br> <br>
						    <p><a href="ItemsBidOn.jsp?UserName=<%=Id%>">Bidding History</a></p>
						    <p><a href="ItemsBought.jsp?UserName=<%=Id%>">Purchase History</a></p>
						    <p><a href="ItemsBeingSold.jsp?UserName<%=Id%>">Items Being Sold</a></p>
							<p><a href="ItemsSold.jsp?UserName=<%=Id%>">Item Sold</a></p>
						<%	

							//String sesUser = ;
							System.out.println(Id + " " + session.getValue("login"));
							int empLevel = -1;// (java.lang.Integer)session.getValue("empLevel");
							if(session.getValue("empLevel") != null)
								empLevel = (java.lang.Integer)session.getValue("empLevel");
							System.out.println(empLevel);
							if(Id.equals((String)session.getValue("login")) )
							{
								%>
										    <p><a href="EditUser.jsp?UserName=<%=Id%>">Edit User</a></p>				
								<%
							}
							else if(empLevel == 1 || empLevel == 2)
							{
								%>
										    <p><a href="EditUser.jsp?UserName=<%=Id%>">Edit User</a></p>				
								<%
							}					
					}
				else
				{
					rs = stmt1.executeQuery("SELECT Employee.StartDate, Employee.EmpLevel, Person.Username, Person.FirstName, Person.Lastname, Person.Address, Person.Zipcode, Person.Email, Person.Telephone, Employee.HourlyRate FROM Person, Employee WHERE  Employee.EmployeeID = Person.SSN AND Person.Username = '" +Id+"'");
					if(rs.next())
					{
								System.out.println(Id + " " + session.getValue("login"));
								int empLevel = -1;// (java.lang.Integer)session.getValue("empLevel");
								if(session.getValue("empLevel") != null)
									empLevel = (java.lang.Integer)session.getValue("empLevel");
								System.out.println(empLevel);
								
								%>
								<p>Start Date: <%=rs.getString(1) %></p>
								<p>Employee Level: <%=rs.getString(2) %></p>
								<p>Name: <%=rs.getString(4) %> <%=rs.getString(5) %></p>
								<p>Address: <%=rs.getString(6) %></p>
								<p>ZipCode: <%=rs.getString(7) %></p>
								<p>Email: <%=rs.getString(8) %></p>
								<p>Telephone: <%=rs.getString(9) %></p>

								
								<%
								if(empLevel == 1)
								{
									%>
																	<p>Hourly Rate: <%=rs.getString(10) %></p><br />
											    <p><a href="EditEmployee.jsp?UserName=<%=Id%>">Edit User</a></p>				
									<%
								}					
						}
					else
						response.sendRedirect("login.jsp");
					
					
				}
  			} catch(Exception e)
			{
				e.printStackTrace();
				out.print(e.toString());
			}
			finally{
			
				try{conn.close();}catch(Exception ee){};
			}
}
  %>
								</table>
			</td>
		</tr>
	</table>
</body>
</html>
